import 'package:flutter/material.dart';
import 'package:isdu_flutter/api/user.dart';

class LoginState extends State<LoginPage> {

  GlobalKey _formKey = new GlobalKey<FormState>();

  TextEditingController _stuNumController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(vertical: 16.0, horizontal: 24.0),
        child: Form(
          key: _formKey,
          child: Column(
            children: <Widget>[
              TextFormField(
                autofocus: true,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                    labelText: "学号",
                    hintText: "请输入12位学号",
                    icon: Icon(Icons.person)
                ),
                validator: (v) {
                  return v.trim().length == 12 ? null : "请输入正确的学号";
                },
                controller: _stuNumController,
              ),

              TextFormField(
                autofocus: true,
                keyboardType: TextInputType.text,
                decoration: InputDecoration(
                    labelText: "密码",
                    hintText: "请输入教务密码",
                    icon: Icon(Icons.lock)
                ),
                obscureText: true,
                validator: (v) {
                  return v.trim().length > 0 ? null : "请输入密码";
                },
                controller: _pwdController,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 28.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: Builder(builder: (context) {
                        return RaisedButton(
                          padding: const EdgeInsets.all(15.0),
                          child: Text("登录"),
                          color: Theme.of(context).primaryColor,
                          textColor: Colors.white,
                          onPressed: () {
                            if (Form.of(context).validate()) {
                              UserAPI.verifyIdentity(
                                context,
                                _stuNumController.text,
                                _pwdController.text, (result, code, msg) {
                                  debugPrint(msg);
                                  });
                            }
                          },
                        );
                      }),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class LoginPage extends StatefulWidget {

  @override
  State createState() => LoginState();

}